package cn.sofwin.web.blog.dao;

import cn.sofwin.core.dao.BaseDao;
import cn.sofwin.core.utils.JdbcUtils;
import cn.sofwin.web.blog.entity.User;

import java.util.List;


/**
 * 用户数据访问层
 * @author ZX
 * @version 1.0
 */
public class UserDao extends BaseDao<User> {
    @Override
    public int insert(User user) {
        String sql = "insert into user(username, password, nickname, avatar, gender) values (?, ?, ?, ?, ?)";
        return JdbcUtils.insert(sql, user.getUsername(), user.getPassword(), user.getNickname(), user.getAvatar(), user.getGender());
    }

    @Override
    public int deleteById(Integer id) {
        return JdbcUtils.update("delete from user where id = ?", id);
    }

    @Override
    public int update(User user) {
        String sql = "update user set password = ?, nickname = ?, avatar = ?, gender = ? where id = ?";
        return JdbcUtils.update(sql, user.getPassword(), user.getNickname(), user.getAvatar(), user.getGender(), user.getId());
    }

    @Override
    public User get(Integer id) {
        return JdbcUtils.queryForObject(User.class, "select * from user where id = ?", id);
    }

    @Override
    public List<User> all() {
        return JdbcUtils.queryForList(User.class, "select * from user");
    }

    public User selectByUsername(String username) {
        String sql="select * from user where username=?";
        return JdbcUtils.queryForObject(User.class,sql,username);
    }
}
